#include "play.h"
#include "stdio.h"
//#include "stdlib.h"

CObjects *ArrayObj[1000];
QuadTree *qTree;
QuadTree *cTree;
QuadTree *mTree;
QuadTree *iTree;
CMushroom * mushroom;
CStar * star;
CJumpingCoin * jumpingCoin;
CPieceBrick *pa1, *pa2, *pa3, *pa4;
CPieceBrick *pb1, *pb2, *pb3, *pb4;

int ArrayIndex;
int cl1, cl2, cl3;
CPlay::CPlay(HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate):
CGame(hInstance,Name,Mode,IsFullScreen, FrameRate)
{
	_d3d = NULL;
	_d3ddv = NULL;
	_BackBuffer = NULL;
}

CPlay::~CPlay()
{

}

void CPlay::LoadMap(char *s)
{
	FILE *f;
	int n,a,b,c,d;
	int x,y;
	ArrayIndex = 0;
	f = fopen(s,"r");
	while(!feof(f))
	{
		fscanf(f,"%d",&n);
		if(n == 0)
		{
			fscanf(f,"%d %d %d",&a,&x,&y);
			ArrayObj[ArrayIndex++] = new CMount(a,x,y);
		}
		else if(n==1)
		{
			fscanf(f,"%d %d %d",&a,&x,&y);
			ArrayObj[ArrayIndex++] = new CGrass(a,x,y);	
		}
		else if(n==2)
		{
			fscanf(f,"%d %d %d",&a,&x,&y);
			ArrayObj[ArrayIndex++] = new CCloud(a,x,y);	
		}
		else if(n==10)
		{
			fscanf(f,"%d %d %d %d %d",&a,&b,&c,&x,&y);
			ArrayObj[ArrayIndex++] = new CBrick(a,b,c,x,y);	
		}
		else if(n==11)
		{
			fscanf(f,"%d %d %d %d %d %d",&a,&b,&c,&d,&x,&y);
			ArrayObj[ArrayIndex++] = new CTube(a,b,c,d,x,y);	
		}
		else if(n==5)
		{
			fscanf(f,"%d %d",&x,&y);
			ArrayObj[ArrayIndex++] = new CCoin(x,y);	
		}
		else if(n == 20)
		{
			fscanf(f,"%d %d %d %d",&a,&b,&x,&y);
			ArrayObj[ArrayIndex++] = new CMush(a,b,x,y);
		}
		else if(n == 21)
		{
			fscanf(f,"%d %d %d",&a,&x,&y);
			ArrayObj[ArrayIndex++] = new CTurtle(a,x,y);		
		}
		else if(n == 3)
		{
			fscanf(f,"%d %d",&x,&y);
			ArrayObj[ArrayIndex++] = new CCastle(x,y);
		}
		else if(n == 30)
		{
			fscanf(f,"%d %d",&x,&y);
			ArrayObj[ArrayIndex++] = new CFlag(x,y);
		}
		else if(n == 50)
		{
			fscanf(f,"%d %d %d",&x,&y,&a);
			cl1 = x;
			cl2 = y;
			cl3 = a;
		}
	}
	for(int i=0;i<ArrayIndex;i++)
	{
		if(ArrayObj[i]->reality < 20)
			qTree->AddPixel(qTree->Root,i,ArrayObj[i]->pos_x,ArrayObj[i]->pos_y);
		else
			mTree->AddPixel(mTree->Root,i,ArrayObj[i]->pos_x,ArrayObj[i]->pos_y);
		if(ArrayObj[i]->reality > 0)
			cTree->Add(ArrayObj[i],i);
	}
	fclose(f);
	time = 0;
}

void CPlay::LoadResources(LPDIRECT3DDEVICE9 _d3ddv)
{
	mario = new CMario();
	mario->LoadResources(_d3ddv);
	//mario->lvUp();
	//mario->lvUp();
	LoadStaticPic(_d3ddv);
	LoadStatusPic(_d3ddv);
	LoadEnemiesPic(_d3ddv);
	LoadBulletPic(_d3ddv); 
	LoadItemPic(_d3ddv);
	LoadFinishPic(_d3ddv);
	LoadoverPic(_d3ddv);
	LoadNumberPic(_d3ddv);
	qTree = new QuadTree(9);
	cTree = new QuadTree(13);
	mTree = new QuadTree(13);
	LoadMap("level1.txt");
}

void CPlay::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int Delta)
{
	d3ddv->ColorFill(_BackBuffer,NULL,D3DCOLOR_XRGB(cl1,cl2,cl3));
	
	if(mushroom->life > 0) 
		mushroom->RenderFrame(d3ddv,Delta);
	if(star->life > 0)
		star->RenderFrame(d3ddv,Delta);
	if(jumpingCoin->life > 0)
		jumpingCoin->RenderFrame(d3ddv,Delta);

	qTree->RenderScreen(scr_x,scr_y,d3ddv,Delta);
	mTree->RenderScreen(scr_x,scr_y,d3ddv,Delta);

	if(pa1->life > 0)
	{
		pa1->RenderFrame(d3ddv,Delta);
		pa2->RenderFrame(d3ddv,Delta);
		pa3->RenderFrame(d3ddv,Delta);
		pa4->RenderFrame(d3ddv,Delta);
	}
	if(pb1->life > 0)
	{
		pb1->RenderFrame(d3ddv,Delta);
		pb2->RenderFrame(d3ddv,Delta);
		pb3->RenderFrame(d3ddv,Delta);
		pb4->RenderFrame(d3ddv,Delta);
	}
	if(mario->inHouse == 0) mario->RenderFrame(d3ddv,Delta);
	else
	{
		SpriteHandlerFP->Begin(D3DXSPRITE_ALPHABLEND);
		FPic->Render(_BackBuffer);
		SpriteHandlerFP->End();
	}
	SpriteHandlerStatusP->Begin(D3DXSPRITE_ALPHABLEND);
		StatusPic->Render(_BackBuffer,0,0);
		SpriteHandlerStatusP->End();
		SpriteHandlerNP->Begin(D3DXSPRITE_ALPHABLEND);
NumberPic->Render(_BackBuffer,235,0,time%10);
NumberPic->Render(_BackBuffer,220,0,(time/10)%10);
NumberPic->Render(_BackBuffer,205,0,(time/100)%10);

		SpriteHandlerNP->End();
		
}

void CPlay::ProcessInput(LPDIRECT3DDEVICE9 d3ddv, int Delta)
{
	if ( mario->state == 10 || mario->came == 1) return;

	if(IsKeyDown(DIK_DOWN))
		mario->SitDown(Delta);
	else
	{
		mario->StandUp();
		if (IsKeyDown(DIK_RIGHT))
			mario->Move(1,Delta);
		else if (IsKeyDown(DIK_LEFT))
			mario->Move(0,Delta);
		else 
			mario->Stop(Delta);
	}
	if(IsKeyDown(DIK_SPACE))
		mario->Jump();
	if(IsKeyDown(DIK_UP))
		mario->JumpShort();
	if(IsKeyDown(DIK_D))
		mario->isRunning = 1;
	else
		mario->isRunning = 0;
}

void CPlay::OnKeyUp(int KeyCode)
{
	if(KeyCode == DIK_SPACE)
		mario->isFalling = 1;
}

void CPlay::OnKeyDown(int KeyCode)
{
	if(KeyCode == DIK_D)
		mario->Shoot();
	if(mario->inHouse == 1)
	{
		if(KeyCode == DIK_RETURN)
			NextLvl();
	}
}

void CPlay::NextLvl()
{
	delete qTree;
	delete cTree;
	delete mTree;
	qTree = new QuadTree(9);
	cTree = new QuadTree(13);
	mTree = new QuadTree(13);
	ArrayIndex = 0;
	LoadMap("level2.txt");
	mario->NewStart();
}